<?php

namespace backend\models\form;

use common\models\Order;
use common\service\BusinessService;
use common\service\ConstatService;
use yii\base\Model;
use Yii ;

class ChangeSignScopeForm extends Model {

    public $distance;


    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['distance'],'required'],
            [['distance'], 'integer'],
            [['distance'],'validDistance'],
        ];
    }

    public function validDistance(){
        if(empty($this->distance)||$this->distance<0){
            $this->addError("distance", "妥投距离不能小于0") ;
            return false ;
        }

    }


    public function attributeLabels() {
        return [
            'distance' => '妥投距离' ,
        ] ;
    }

    public  function changeDistance($orderno){
        if(empty($orderno)){
            return ["code"=>400,"msg"=>"订单号不能空"];
        }

        $order = Order::findOne(["orderno"=>$orderno]) ;

        if(empty($order)){
            return ["code"=>400,"msg"=>"不存在该订单"];
        }

        if($order->status != ConstatService::ORDER_DISPATCHING){
            return ["code"=>400,"msg"=>"状态不是配送，无法修改"];
        }

        if(!empty(Yii::$app->admin->identity->store_code)){
            if(Yii::$app->admin->identity->store_code!=$order['store_code']){
                return ["code"=>400,"msg"=>"无法操作其它门店的数据"];
            }
        };

        $order->sign_scope = $this->distance ;
        $rs = $order->save() ;
        if(empty($rs)){
            $msg = current($order->getFirstErrors());
            return ["code"=>400,"msg"=>"失败,原因:".$msg];
        }

        $logArr['orderno'] = $orderno ;
        BusinessService::addSignDistancelog($logArr);

        return ["code"=>200,"msg"=>"成功"];


    }



}
